<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="serach">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.name"
            placeholder="花园名"
            clearable
          ></el-input>
          <el-button
            type="success"
            plain
            icon="el-icon-search"
            @click="getFlowerList"
            >搜索</el-button
          >
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="gardenList" style="width: 100%" border>
        <el-table-column label="#" width="50">
          <template slot-scope="scope">
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + 1 + scope.$index
            }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="花属id" width="80"> </el-table-column>
        <el-table-column prop="images" label="图片" width="90">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 80px; height: 80px"
                :src="scope.row.images"
              >
              </el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="花名" width="80"> </el-table-column>
        <el-table-column prop="ename" label="英文名" width="100">
        </el-table-column>
        <el-table-column prop="families" label="科" width="80">
        </el-table-column>
        <el-table-column prop="efamilies" label="属" width="80">
        </el-table-column>
        <el-table-column prop="apply" label="应用" width="200">
        </el-table-column>
        <el-table-column
          prop="feature"
          label="简介"
          width="280"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              plain
              icon="el-icon-plus"
              circle
              @click="openEditUi(null)"
            ></el-button>
            <el-button
              plain
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="openEditUi(scope.row.id)"
            ></el-button>
            <el-button
              plain
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="delFlower(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增对话框 -->
    <el-dialog
      @close="clearFrom"
      :title="title"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="Flowerform" :rules="rules" ref="FlowerFormRef">
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-form-item
              label="花名"
              :label-width="formLabelWidth"
              prop="name"
            >
              <el-input
                v-model="Flowerform.name"
                autocomplete="off"
                width="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="英文名"
              :label-width="formLabelWidth"
              prop="ename"
            >
              <el-input
                v-model="Flowerform.ename"
                autocomplete="off"
                width="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="科"
              :label-width="formLabelWidth"
              prop="families"
            >
              <el-input
                v-model="Flowerform.families"
                autocomplete="off"
                width="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1.5">
            <el-form-item
              label="属"
              :label-width="formLabelWidth"
              prop="efamilies"
            >
              <el-input
                v-model="Flowerform.efamilies"
                autocomplete="off"
                width="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item
              label="应用"
              :label-width="formLabelWidth"
              prop="apply"
            >
              <el-input
                v-model="Flowerform.apply"
                autocomplete="off"
                width="200"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item
              label="简介"
              :label-width="formLabelWidth"
              prop="feature"
            >
              <el-input
                v-model="Flowerform.feature"
                autocomplete="off"
                width="200"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="上传图片url地址"
              :label-width="formLabelWidth"
              prop="iamges"
            >
              <el-input
                v-model="Flowerform.iamges"
                autocomplete="off"
                width="200"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="success" @click="saveuser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import gardenApi from "@/api/garden";
export default {
  data() {
    return {
      formLabelWidth: "130px",
      Flowerform: {},
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      total: 0,
      gardenList: [],
      title: "",
      dialogFormVisible: false,
      dialogFormVisiblee: false,
      rules: {
        name: [{ required: true, message: "请输入花名", trigger: "blur" }],
        ename: [{ required: true, message: "请输入english", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getFlowerList();
  },
  methods: {
    //   获取花花园列表,刷新
    getFlowerList() {
      gardenApi.getFlowerList(this.searchModel).then((Response) => {
        this.gardenList = Response.data.rows;
        this.total = Response.data.total;
      });
    },
    // 分页逻辑
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getFlowerList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getFlowerList();
    },
    // 打开新增/删除
    openEditUi(id) {
      if (id == null) {
        this.title = "新增花园";
      } else {
        this.title = "修改花属";
        // 根据id查询用户id
        gardenApi.getFlowerById(id).then((response) => {
          this.Flowerform = response.data;
        });
      }
      this.dialogFormVisible = true;
    },
    clearFrom() {
      this.Flowerform = {};
      this.$refs.FlowerFormRef.clearValidate();
    },
    // 保存表单
    saveuser() {
      this.$refs.FlowerFormRef.validate((valid) => {
        if (valid) {
          gardenApi.saveFlower(this.Flowerform).then((response) => {
            this.$message({
              showClose: true,
              message: response.message,
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getFlowerList();
          });
        } else {
          this.$message({
            showClose: true,
            message: "输入表单！",
            type: "warning",
          });
          return false;
        }
      });
    },
    // 删除
    delFlower(garden) {
        this.$confirm(`您确认删除用户${garden.name}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            gardenApi.deltFlowerById(garden.id).then(response =>{
                this.$message({
                type: 'success',
                message: response.message,
          });
          this.getFlowerList();
            }

            )
         
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
  },
};
</script>
  
  <style>
#serach .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 85%;
}
</style>